define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wanlshop/settlement/index' + location.search,
                    add_url: 'wanlshop/settlement/add',
                    edit_url: 'wanlshop/settlement/edit',
                    del_url: 'wanlshop/settlement/del',
                    multi_url: 'wanlshop/settlement/multi',
                    import_url: 'wanlshop/settlement/import',
                    table: 'wanlshop_settlement',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('订单号')},
                        {field: 'package_no', title: __('包裹号')},
                        {field: 'package.freight_price', title: __('运费成本')},
                        {field: 'package.package_price', title: __('货品销售总额')},
                        {field: 'package.package_supply_price', title: __('供货总价')},
                        {field: 'package.service_price', title: __('服务费')},
                        {field: 'package.settlement_price', title: __('结算金额')},
                        {field: 'package.delivery_status',title: __('包裹状态'),searchList: {"0":__('未处理'),"1":__('已处理')}, formatter: Table.api.formatter.normal},
                        {field: 'way', title: __('Way'), searchList: {"auto":__('Way auto'),"admin":__('Way admin')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('结算状态'), searchList: {"created":__('Status created'),"success":__('Status success'),"lock":__('Status lock')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'successtime', title: __('Successtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看'),
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'wanlshop/settlement/detail',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            //  订单导出
            var submitForm = function (ids,operate,confirm) {
                var options = table.bootstrapTable('getOptions');
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                    if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                        columns.push(j.field);
                    }
                });
                var search = options.queryParams({});

                var loadding = Layer.load();
                Fast.api.ajax({
                    'url' : 'wanlshop/settlement/multiSettlement',
                    'data':{
                        ids:ids,
                        operate:operate,
                        search:options.searchText,
                        filter:search.filter,
                        op:search.op,
                        columns:columns.join(','),
                    }
                },function () {
                    Layer.closeAll();
                    $(".btn-refresh").trigger("click");
                },function () {
                    Layer.close(loadding);
                })
            };

            $(document).on("click", ".btn-settlement", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                var operate = $(this).data('operate');
                console.log(ids, page, all);
                Layer.confirm("确定进行此操作?", {
                    title: '请选择操作范围',
                    btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    }
                    , yes: function (index, layero) {
                        submitForm(ids.join(","), operate,layero);
                        return false;
                    }
                    ,
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        submitForm(ids.join(","), operate,layero);
                        return false;
                    }
                    ,
                    btn3: function (index, layero) {
                        submitForm("all", operate,layero);
                        return false;
                    }
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $('.btn-settlement').click(function () {
                var ids = $(this).data('id');
                var operate = $(this).data('operate');
                var confirm = Layer.confirm('确定进行此操作?',
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                    function (i) {
                        var loadding = Layer.load();
                        Fast.api.ajax({
                            'url' : 'wanlshop/settlement/settlement',
                            'data':{
                                ids:ids,
                                operate:operate
                            }
                        },function () {
                            Layer.close(confirm);
                            Layer.close(loadding);
                            window.location.reload();
                        },function () {
                            Layer.close(confirm);
                            Layer.close(loadding);
                        })
                    }
                );
            });

            $('.btn-lock').click(function () {
                var id = $(this).data('id');
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});